package com.yuntu.service;

import com.yuntu.dao.DevMapper;
import com.yuntu.pojo.Dev;
import com.yuntu.tools.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class DevServiceImpl implements DevService{
    @Autowired
    private DevMapper devMapper;

    @Override
    public Dev login(String devCode, String devPassword) throws Exception {
        Dev user=devMapper.getLoginUser(devCode);
        if (null!=user){
            if(!user.getDevPassword().equals(devPassword)){
                user=null;
            }
        }
        return user;
    }

    @Override
    public List<Dev> devAll() throws Exception {
        List<Dev> peitemsList=devMapper.getDevList();
        return peitemsList;
    }

    @Override
    public boolean add(Dev dev) throws Exception {
        boolean flag=false;
        int count=devMapper.addDevType(dev);
        if (count>0){
            flag=true;
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败!");
        }
        return flag;
    }

    @Override
    public boolean del(int id) throws Exception {
        boolean flag=false;
        int count=devMapper.deleteDevById(id);
        if (count > 0){
            flag = true;
            System.out.println("删除成功!");
        }else {
            System.out.println("删除失败!");
        }
        return flag;
    }

    @Override
    public Dev getId(int id) throws Exception {
        Dev peitems=null;
        peitems = devMapper.getDevById(id);
        return peitems;
    }

    @Override
    public boolean upd(Dev dev) throws Exception {
        boolean flag=false;
        int count=devMapper.updateDev(dev);
        if (count > 0){
            flag = true;
            System.out.println("修改成功!");
        }else {
            System.out.println("修改失败!");
        }
        return flag;
    }

    @Override
    public List<Dev> getPageUtil(PageUtil<Dev> pageUtil) throws Exception {
        List<Dev> peitemsList=new ArrayList<>();
        int count=devMapper.getCount();
        pageUtil.setCounts(count);
        if (count>0){
            if (pageUtil.getPageindex()>pageUtil.getPagecount()){
                pageUtil.setPageindex(pageUtil.getPagecount());
            }
            peitemsList=devMapper.getPageAll(pageUtil);
            pageUtil.setLists(peitemsList);
        }
        return peitemsList;
    }
}
